package com.example.administrator.nurse.present;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.nurse.R;
import com.example.administrator.nurse.base.BaseActivity;
import com.example.administrator.nurse.widget.nativeCharts.SplineChart01View;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by 林冠宏 on 2016/4/26.
 * 曲线图
 *
 */

public class DataPicActivity extends BaseActivity implements View.OnClickListener{

    private SplineChart01View qx1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        configCommonHeader(this,"趋势图",1);
        initView();
    }

    @Override
    protected int setLayotId() {
        return R.layout.data_pic_main;
    }

    private void initView(){

        qx1 = (SplineChart01View) f(R.id.chart);
        qx1.initView();
        qx1.withStraightLine()
                .withBigData(true)
                .show(new double[]{0,20,80,60,260,330,80,77,4});

        LinearLayout firstFather = (LinearLayout) f(R.id.firstFather);
        LinearLayout secondFather = (LinearLayout) f(R.id.secondFather);
        LinearLayout threeFather = (LinearLayout) f(R.id.threeFather);
        LinearLayout fourFather = (LinearLayout) f(R.id.fourFather);
        LinearLayout fiveFather = (LinearLayout) f(R.id.fiveFather);

        View decor = getWindow().getDecorView();

        /** 第一组 */
        final View view1 = LayoutInflater.from(this).inflate(R.layout.select_title_item, firstFather, false);
        final View view2 = LayoutInflater.from(this).inflate(R.layout.select_title_item, firstFather, false);
        final View view3 = LayoutInflater.from(this).inflate(R.layout.select_title_item, firstFather, false);
        final View view4 = LayoutInflater.from(this).inflate(R.layout.select_title_item, firstFather, false);

        configItemView(view1,"心率",false);
        configItemView(view2,"血压",true);
        configItemView(view3,"血糖",false);
        configItemView(view4,"体温",false);

        view1.setOnClickListener(new ItemClick(0,new itemClickCallBack() { /** 心率 */
            @Override
            public void onCallBack(View v) {

            }

        },view1,view2,view3,view4));
        view2.setOnClickListener(new ItemClick(1,new itemClickCallBack() { /** 血压 */
            @Override
            public void onCallBack(View v) {

            }

        },view1,view2,view3,view4));
        view3.setOnClickListener(new ItemClick(2,new itemClickCallBack() { /** 血糖 */
            @Override
            public void onCallBack(View v) {

            }

        },view1,view2,view3,view4));
        view4.setOnClickListener(new ItemClick(3,new itemClickCallBack() { /** 体温 */
            @Override
            public void onCallBack(View v) {

            }

        },view1,view2,view3,view4));

        firstFather.addView(view1);
        firstFather.addView(view2);
        firstFather.addView(view3);
        firstFather.addView(view4);

        /** 第二组 */
        final View view11 = LayoutInflater.from(this).inflate(R.layout.select_title_item, secondFather, false);
        final View view21 = LayoutInflater.from(this).inflate(R.layout.select_title_item, secondFather, false);
        final View view31 = LayoutInflater.from(this).inflate(R.layout.select_title_item, secondFather, false);
        final View view41 = LayoutInflater.from(this).inflate(R.layout.select_title_item, secondFather, false);

        configItemView(view11,"日志",false);
        configItemView(view21,"曲线",true);
        configItemView(view31,"表格",false);
        configItemView(view41,"总览", false);

        view11.setOnClickListener(new ItemClick(0, new itemClickCallBack() {/** 日志 */
            @Override
            public void onCallBack(View v) {

            }
        }, view11, view21, view31, view41));
        view21.setOnClickListener(new ItemClick(1,new itemClickCallBack() {/** 曲线 */
            @Override
            public void onCallBack(View v) {

            }
        },view11, view21, view31, view41));
        view31.setOnClickListener(new ItemClick(2,new itemClickCallBack() {/** 表格 */
            @Override
            public void onCallBack(View v) {

            }
        },view11, view21, view31, view41));
        view41.setOnClickListener(new ItemClick(3,new itemClickCallBack() {/** 总览 */
            @Override
            public void onCallBack(View v) {

            }
        },view11, view21, view31, view41));

        secondFather.addView(view11);
        secondFather.addView(view21);
        secondFather.addView(view31);
        secondFather.addView(view41);

        /** 第三组 */
        final View view111 = LayoutInflater.from(this).inflate(R.layout.select_title_item, fiveFather, false);
        final View view211 = LayoutInflater.from(this).inflate(R.layout.select_title_item, fiveFather, false);
        final View view311 = LayoutInflater.from(this).inflate(R.layout.select_title_item, fiveFather, false);
        final View view411 = LayoutInflater.from(this).inflate(R.layout.select_title_item, fiveFather, false);

        configItemView(view111,"年",false);
        configItemView(view211,"月",true);
        configItemView(view311,"周",false);
        configItemView(view411,"日",false);

        view111.setOnClickListener(new ItemClick(0,new itemClickCallBack() {/** 年 */
            @Override
            public void onCallBack(View v) {

            }
        },view111, view211, view311, view411));
        view211.setOnClickListener(new ItemClick(1,new itemClickCallBack() {/** 月 */
            @Override
            public void onCallBack(View v) {

            }
        },view111, view211, view311, view411));
        view311.setOnClickListener(new ItemClick(2,new itemClickCallBack() {/** 周 */
            @Override
            public void onCallBack(View v) {

            }
        },view111, view211, view311, view411));
        view411.setOnClickListener(new ItemClick(3,new itemClickCallBack() {/** 日 */
            @Override
            public void onCallBack(View v) {

            }
        },view111, view211, view311, view411));

        fiveFather.addView(view111);
        fiveFather.addView(view211);
        fiveFather.addView(view311);
        fiveFather.addView(view411);

        /** 单位组 */
        final View view1111 = LayoutInflater.from(this).inflate(R.layout.data_danwei_item, fourFather, false);
        final View view2111 = LayoutInflater.from(this).inflate(R.layout.data_danwei_item, fourFather, false);
        //final View view3111 = LayoutInflater.from(this).inflate(R.layout.data_danwei_item, (ViewGroup) decor, false);
        //final View view4111 = LayoutInflater.from(this).inflate(R.layout.data_danwei_item, (ViewGroup) decor, false);

        configDanWeiItemView(view1111, "舒张压 (mmhg)", new itemClickCallBack() {
            @Override
            public void onCallBack(View v) {

            }
        });
        configDanWeiItemView(view2111,"收缩压 (mmhg)",new itemClickCallBack() {
            @Override
            public void onCallBack(View v) {

            }
        });
        fourFather.addView(view1111);
        fourFather.addView(view2111);

    }

    /** 单位类适配操作 */
    private void configDanWeiItemView(View view,String title,final itemClickCallBack itemClickCallBack){
        ((TextView)view.findViewById(R.id.text)).setText(title);
        view.findViewById(R.id.image).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /** 更改 iamge 图标写下面 */
                if(itemClickCallBack !=null){
                    itemClickCallBack.onCallBack(v);
                }
            }
        });
    }


    /** 标题类选择适配操作 */
    private interface itemClickCallBack{
        void onCallBack(View v);
    }

    private class ItemClick implements View.OnClickListener{

        private int index = 0;
        private Map<TextView,View>[] maps = null;
        private itemClickCallBack itemClickCallBack;

        public ItemClick(int index,itemClickCallBack itemClickCallBack,View... views){
            int length = views.length;
            maps = new Map[length];
            for(int i =0;i<length;i++)
                maps[i] = (getMap((TextView) views[i].findViewById(R.id.text),(views[i].findViewById(R.id.line))));
            this.itemClickCallBack = itemClickCallBack;
            this.index = index;
        }

        @Override
        public void onClick(View v) {
            configItemView(index,maps);
            if(itemClickCallBack!=null){
                itemClickCallBack.onCallBack(v);
            }
        }
    }

    private static <T extends View>  List<T> getList(T... views){
        List<T> conViews = new ArrayList<>();
        int length = views.length;
        for(int i=0;i<length;i++)
            conViews.add(views[i]);

        return  conViews;
    }

    private static <K,V> Map<K,V> getMap(K tv,V iv){
        Map<K,V> itemMap = new HashMap<>();
        itemMap.put(tv, iv);
        return itemMap;
    }

    private void configItemView(View view,String title,boolean isShowLine){
        ((TextView)view.findViewById(R.id.text)).setText(title);
        if(isShowLine)
            view.findViewById(R.id.line).setVisibility(View.VISIBLE);
    }

    private<T extends View> void configItemView(
            int index,
            //List<T> list,
            Map<TextView,View>[] views
    ){
        int length = views.length,status;
        TextView key = null;
        View value = null;
        Set set;
        for(int k =0;k<length;k++){
            set = views[k].keySet();
            for(Object aSet :set){
                key = (TextView) aSet;
                value = views[k].get(key);
            }
            if(k==index){
                key.setTextColor(getResources().getColor(R.color.data_line));
                status = View.VISIBLE;
            }else{
                key.setTextColor(getResources().getColor(R.color.data_text));
                status = View.INVISIBLE;
            }
            value.setVisibility(status);
        }

    }

    @Override
    protected void onFailure(int requestCode, Object object, String errorMsg) {

    }

    @Override
    protected void onSuccess(int requestCode, Object object, String response) {

    }

    @Override
    public void onClick(View v) {

    }
}
